import {defineStore} from 'pinia'
import {getToken, setToken, removeToken} from '@/utils/storage.ts'
import {login,getIndex} from "@/api/user";
interface LoginData {
    loginName: string
    password: string
}
const { ipcRenderer } = require('electron');
import router  from '@/router/index'
//1.创建store
// store实例相当于一个容器，里面存放的有类似于data，计算属性，方法之类的东西。通过defineStore()方法定义
// 在src下面创建一个store文件夹，再创建与之对应的js文件，比如user.js
// 在pinia中，你可以认为 state 是 store 的数据 (data)，getters 是 store 的计算属性 (computed)，而 actions 则是方法 (methods)，相比vuex，pinia没有mutations
export const useUserStore = defineStore('user', {
    state: () => {
        return {
            token: getToken("Access-Token"), // 登录信息
            // routerList: [], // 路由权限
            init:false,//判断是否获取过路由
            userInfo: {} // 用户信息
        }
    },
    actions: {
        userLogin(loginData:LoginData) {
            const data = {
                // 'user': JSON.stringify(userInfo),
                'loginName': loginData.loginName,
                'password': loginData.password
            }
            return new Promise((resolve) => {
                login(data).then((response) => {
                    const {data} = response
                    this.token = data.accessToken;
                    setToken("Access-Token",data.accessToken)
                    resolve(response)
                }).catch((error) => {
                    resolve(error)
                })
            })
        },
        getInfo(){
            return new Promise((resolve, reject) => {
                // getIndex().then((response:any) => {
                //     const {data} = response
                //     console.log("222",response);
                //     this.userInfo = response.user

                //     resolve(data)
                // }).catch(error => {
                //     console.log(error);
                //     this.init = false
                //     reject(error)
                // })
                const response1 = JSON.parse(JSON.stringify({
                    "httpCode": 200,
                    "message": "成功",
                    "data": {
                        "menus": [
                            {
                                "menuType": "M",
                                "url": "/system",
                                "name": "",
                                "icon": "Setting",
                                "menuName": "系统管理",
                                "children": [
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "name": "Menu",
                                        "url": "/system/menu",
                                        "icon": "",
                                        "menuName": "菜单管理"
                                    },
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "name": "Role",
                                        "url": "/system/role",
                                        "icon": "",
                                        "menuName": "角色管理"
                                    },
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "name": "User",
                                        "url": "/system/user",
                                        "icon": "",
                                        "menuName": "用户管理"
                                    },
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "name": "Dept",
                                        "url": "/system/dept",
                                        "icon": "",
                                        "menuName": "部门管理"
                                    }
                                ]
                            },
                            {
                                "menuType": "M",
                                "url": "/system",
                                "icon": "User",
                                "name": "",
                                "menuName": "客户管理",
                                "children": null
                            },
                            {
                                "menuType": "M",
                                "url": "/directives",
                                "name": "",
                                "icon": "CollectionTag",
                                "menuName": "自定义指令",
                                "children": [
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/directives/thrShaking",
                                        "name": "ThrShaking",
                                        "icon": "",
                                        "menuName": "节流防抖"
                                    }
                                ]
                            },
                            {
                                "menuType": "M",
                                "url": "/table",
                                "name": "",
                                "icon": "Grid",
                                "menuName": "表格",
                                "children": [
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/table/packTable",
                                        "name": "PackTable",
                                        "icon": "",
                                        "menuName": "表格基本封装"
                                    },
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/table/userList",
                                        "name": "UserTable",
                                        "icon": "",
                                        "menuName": "表格封装"
                                    },
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/table/dragableTable",
                                        "name": "DragableTable",
                                        "icon": "",
                                        "menuName": "表格拖拽"
                                    },
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/table/editTable",
                                        "name": " EditTable",
                                        "icon": "",
                                        "menuName": "表格编辑"
                                    },
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/table/contextTable",
                                        "name": "ContextTable",
                                        "icon": "",
                                        "menuName": "表格右键菜单"
                                    }
                                ]
                            },
                            {
                                "menuType": "M",
                                "url": "/form",
                                "name": "",
                                "icon": "Tickets",
                                "menuName": "表单",
                                "children": [
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/form/simpleForm",
                                        "name": "SimpleForm",
                                        "icon": "",
                                        "menuName": "表单封装"
                                    }
                                ]
                            },
                            {
                                "menuType": "M",
                                "url": "/dialog",
                                "name": "",
                                "icon": "Notification",
                                "menuName": "对话框",
                                "children": [
                                    {
                                        "children": null,
                                        "menuType": "C",
                                        "url": "/dialog/simpleDialog",
                                        "name": "SimpleDialog",
                                        "icon": "",
                                        "menuName": "Dialog封装"
                                    }
                                ]
                            },
                            {
                                "menuType": "M",
                                "name": "",
                                "url": "/system",
                                "icon": "Document",
                                "menuName": "订单管理",
                                "children": null
                            }
                        ]
                    },
                    "user": {
                        "userName": "系统管理员",
                        "loginName": "admin"
                    },
                    "ok": true
                }))
                console.log(response1)
                this.userInfo = {loginName:'admin',userName:'管理员'}
                resolve(response1.data)
            })
        },
        logout() {
            return new Promise(() => {
                // this.token = ''
                // this.init = false
                // this.userInfo = {};
                ipcRenderer.send('showLoginWindow');
                // removeToken("Access-Token")
                // window.location.reload()
                router.push('/login')
            })
        }
    }
})
// import { defineStore } from 'pinia'
// import { getToken, setToken, removeToken } from '@/utils/storage'
// //1.创建store
// // store实例相当于一个容器，里面存放的有类似于data，计算属性，方法之类的东西。通过defineStore()方法定义
// // 在src下面创建一个store文件夹，再创建与之对应的js文件，比如user.js
// console.log(defineStore);
// export const useUserStore = defineStore({
//     id: 'user', // id必填，且需要唯一
//     state: () => {
//         return {
//             name: '以和为贵',
//             age: 20
//         }
//     },
//     actions: {
//         updateName(name: string) {
//             this.name = name
//         },
//         updateAge(age: number) {
//             this.age = age
//         }
//     }
// })
